package cn.iocoder.yudao.module.dispatch.controller.admin.runningstate;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dispatch.controller.admin.runningstate.vo.*;
import cn.iocoder.yudao.module.dispatch.dal.dataobject.runningstate.RunningStateDO;
import cn.iocoder.yudao.module.dispatch.convert.runningstate.RunningStateConvert;
import cn.iocoder.yudao.module.dispatch.service.runningstate.RunningStateService;

@Api(tags = "运行调度 - 参数")
@RestController
@RequestMapping("/dispatch/running-state")
@Validated
public class RunningStateController {

    @Resource
    private RunningStateService runningStateService;

    @GetMapping("/findAll")
    @ApiOperation("findAll")
    @ApiImplicitParam(name = "page", value = "page", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('dispatch:running-state:query')")
    public CommonResult<List<RunningStateDO>> findAll() {
        List<RunningStateDO> list = runningStateService.findAll();
        return success(list);
    }

    @GetMapping("/findByPage")
    @ApiOperation("findByPage")
    @ApiImplicitParam(name = "page", value = "page", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('dispatch:running-state:query')")
    public CommonResult<List<RunningStateDO>> findByPage(@RequestParam("page") Integer page) {
        List<RunningStateDO> list = runningStateService.findByPage(page);
        return success(list);
    }

    @PostMapping("/create")
    @ApiOperation("创建参数")
    @PreAuthorize("@ss.hasPermission('dispatch:running-state:create')")
    public CommonResult<Integer> createRunningState(@Valid @RequestBody RunningStateCreateReqVO createReqVO) {
        return success(runningStateService.createRunningState(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新参数")
    @PreAuthorize("@ss.hasPermission('dispatch:running-state:update')")
    public CommonResult<Boolean> updateRunningState(@Valid @RequestBody RunningStateUpdateReqVO updateReqVO) {
        runningStateService.updateRunningState(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除参数")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('dispatch:running-state:delete')")
    public CommonResult<Boolean> deleteRunningState(@RequestParam("id") Integer id) {
        runningStateService.deleteRunningState(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得参数")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('dispatch:running-state:query')")
    public CommonResult<RunningStateRespVO> getRunningState(@RequestParam("id") Integer id) {
        RunningStateDO runningState = runningStateService.getRunningState(id);
        return success(RunningStateConvert.INSTANCE.convert(runningState));
    }

    @GetMapping("/list")
    @ApiOperation("获得参数列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('dispatch:running-state:query')")
    public CommonResult<List<RunningStateRespVO>> getRunningStateList(@RequestParam("ids") Collection<Integer> ids) {
        List<RunningStateDO> list = runningStateService.getRunningStateList(ids);
        return success(RunningStateConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得参数分页")
    @PreAuthorize("@ss.hasPermission('dispatch:running-state:query')")
    public CommonResult<PageResult<RunningStateRespVO>> getRunningStatePage(@Valid RunningStatePageReqVO pageVO) {
        PageResult<RunningStateDO> pageResult = runningStateService.getRunningStatePage(pageVO);
        return success(RunningStateConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出参数 Excel")
    @PreAuthorize("@ss.hasPermission('dispatch:running-state:export')")
    @OperateLog(type = EXPORT)
    public void exportRunningStateExcel(@Valid RunningStateExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<RunningStateDO> list = runningStateService.getRunningStateList(exportReqVO);
        // 导出 Excel
        List<RunningStateExcelVO> datas = RunningStateConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "参数.xls", "数据", RunningStateExcelVO.class, datas);
    }

}
